﻿// <copyright file="App.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-27</date>
// <summary>Application class</summary>

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Silverlight.Testing;

    /// <summary>
    /// Test Application
    /// </summary>
    public partial class App : Application
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the App class
        /// </summary>
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }
        #endregion

        #region Implementation
        /// <summary>
        /// Application Startup event handler
        /// </summary>
        /// <param name="sender">this application</param>
        /// <param name="e">the startup event arguments</param>
        /// <remarks>If the InitParams contains the parameter APIDocResource, then show the documentation page for the API, using a resource named in the value</remarks>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Load the main control
            if (e.InitParams.ContainsKey("APIDocResource"))
            {
                var resourceName = e.InitParams["APIDocResource"];

                this.RootVisual = new DocumentationPage(resourceName);
            }
            else
            {
                UnitTestSettings settings = UnitTestSystem.CreateDefaultSettings();

                ////settings.TestService = null;

                this.RootVisual = UnitTestSystem.CreateTestPage(settings);
            }
        }

        /// <summary>
        /// Application Exit event handler
        /// </summary>
        /// <param name="sender">the application object</param>
        /// <param name="e">exit event args</param>
        private void Application_Exit(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Application unhandled exception event handler
        /// </summary>
        /// <param name="sender">the application</param>
        /// <param name="e">unhandled exception</param>
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.ExceptionObject);

            if (!System.Diagnostics.Debugger.IsAttached)
            {
                e.Handled = true;

                try
                {
                    string errorMsg = e.ExceptionObject.Message + @"\n" + e.ExceptionObject.StackTrace;
                    errorMsg = errorMsg.Replace("\"", "\\\"").Replace("\r\n", @"\n");

                    System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application: " + errorMsg + "\");");
                }
                catch (Exception)
                {
                }
            }
        }

        #endregion
    }
}
